package com.perye.dokit.workcontroller;

import com.perye.dokit.aop.log.Log;
import com.perye.dokit.base.BaseController;
import com.perye.dokit.entity.TestLi;
import com.perye.dokit.newservice.BaseService;
import com.perye.dokit.newservice.HqPhotoService;
import com.perye.dokit.newservice.HqVideoService;
import com.perye.dokit.utils.DateUtils;
import com.perye.dokit.utils.StringUtils;
import com.perye.dokit.utils.pageData.PageData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: chenhao
 * \* Date: 2021/7/19
 * \* Time: 11:13
 * \* Description: 基础信息模块
 * \
 */

@RestController
@Api(tags = "hq:基础管理")
@RequestMapping("/api/wedding")
public class HqBaseController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(HqBaseController.class);
    @Autowired
    private HqPhotoService photoService;
    @Autowired
    private HqVideoService videoService;
    @Autowired
    private BaseService baseService;


    @ResponseBody
    public ResponseEntity<Object> delPicture() throws Exception {
        PageData result = new PageData();
        PageData pageData = getPageData();

        try {
            String[] ids = new String[]{};


        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new Exception(e.getMessage());
        }
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
    @Log("删除")
    @ApiOperation("删除")
    @RequestMapping("/deleteVideo")
    public ResponseEntity<Object> deleteVideo(@RequestBody String[] ids) throws Exception {
        videoService.deleteVideo(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @Log("查询wedding")
    @ApiOperation("查询wedding")
    @GetMapping
    @PreAuthorize("@dokit.check('wedding:list')")
    @ResponseBody
    public ResponseEntity<Object> list() throws Exception {
        PageData result = new PageData();
        PageData pageData = getPageData();

        try {

            List<PageData> list = baseService.getListBase(pageData);
            result.put("content", list);
            result.put("status", 0);
            result.put("msg", "查询成功");
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new Exception(e.getMessage());
        }
        return new ResponseEntity<>(result, HttpStatus.OK);
    }


    @Log("新增wedding")
    @ApiOperation("新增wedding")
    @PostMapping
    @PreAuthorize("@dokit.check('wedding:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody PageData pageData) throws Exception {

        pageData.put("createTime", DateUtils.getDate());
        /*   pageData.put("id",this.get32UUID());*/
        Integer id = baseService.create(pageData);


        //上传视频
        PageData videoData = new PageData(); //图片对象
        videoData.put("url", pageData.getString("videoUrl")); //图片url
        videoData.put("length", pageData.getString("vlength")); //图片length
        videoData.put("width", pageData.getString("vwidth")); //图片width
        videoData.put("createTime", DateUtils.getDate());
        videoData.put("typeId", id);
        videoData.put("type", "lunbo"); //图片类型
        videoService.create(videoData);
        //图片上传

        List<Map<String, Object>> picutureList = (List<Map<String, Object>>) pageData.get("fileList");
        List<Map<String, Object>> newAddmsList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < picutureList.size(); i++) {
            PageData data = new PageData();
            data.put("createTime", DateUtils.getDate());
            data.put("url", picutureList.get(i).get("url")); //图片url
            data.put("length", "0"); //图片length
            data.put("width", "0"); //图片width
            data.put("sort", i); //图片排序
            data.put("typeId", id);
            data.put("type", "lunbo"); //图片类型
            newAddmsList.add(data);
        }


        if (newAddmsList.size() > 0) {
            photoService.addPictureList(newAddmsList);
        }


        return new ResponseEntity<>(pageData, HttpStatus.CREATED);
    }

    @Log("修改wedding")
    @ApiOperation("修改wedding")
    @PutMapping
    @PreAuthorize("@dokit.check('wedding:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody PageData pageData) throws Exception {
        baseService.update(pageData);

        List<Map<String, Object>> picutureList = (List<Map<String, Object>>) pageData.get("fileList");
        List<Map<String, Object>> newAddmsList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < picutureList.size(); i++) {
            if (StringUtils.equals("add", picutureList.get(i).get("type") + "")) {
                PageData data = new PageData();
                data.put("createTime", DateUtils.getDate());
                data.put("url", picutureList.get(i).get("url")); //图片url
                data.put("length", "0"); //图片length
                data.put("width", "0"); //图片width
                data.put("sort", i); //图片排序
                data.put("typeId", pageData.get("id"));
                data.put("type", "lunbo"); //图片类型
                newAddmsList.add(data);
            }
        }

        //上传视频
        PageData videoData = new PageData(); //图片对象
        videoData.put("url", pageData.getString("videoUrl")); //图片url
        videoData.put("length", 0); //图片length
        videoData.put("width", 0); //图片width
        videoData.put("createTime", DateUtils.getDate());
        videoData.put("typeId", pageData.get("id"));
        videoData.put("type", "lunbo"); //图片类型
        videoService.create(videoData);
        //图片上传

        if (newAddmsList.size() > 0) {
            photoService.addPictureList(newAddmsList);
        }


        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除wedding")
    @ApiOperation("删除wedding")
    @PreAuthorize("@dokit.check('wedding:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody String[] ids) throws Exception {
        baseService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}